/* eslint no-constant-condition: ["error", { "checkLoops": false }] */
/**
 *
 * Copyright 2016-present scrm
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
import { put, take, call, fork } from 'redux-saga/effects';

import * as types from '../constants/ActionTypes';
import ToastUtil from '../utils/ToastUtil';
import RequestXhrUtil from '../utils/RequestXhrUtil';
import { SCRM_API } from '../constants/Urls';
import { UA } from '../utils/AttachParamsUtil';
import { 
  fetchVerifySignature, 
  receiveVerifySignature, 
  fetchToken,  
  receiveToken
} from '../actions/auth';

export function* requestVerifySignature(
  signature,
  nonceStr,
  timeStamp,
  message
) {
  try {
    yield put(fetchVerifySignature(signature, nonceStr, timeStamp));
    const verifyResponse = yield call(
      RequestXhrUtil.request,
      `${SCRM_API}/verifySignature`,
      'post',
      UA({
        signature: signature,
        nonce: nonceStr,
        timestamp: timeStamp
      })
    );
    yield put(receiveVerifySignature(
      verifyResponse,
      message
    ));
    const errorMessage = verifyResponse.errmsg;
    if (verifyResponse.errcode) {
      yield ToastUtil.showShort(errorMessage);
    }
  } catch (error) {
    yield put(receiveVerifySignature(null, message));
    ToastUtil.showShort('网络发生错误，请重试');
  }
}

export function* requestToken(
  site_id,
  uniqueIdMd5,
  uid,
  timeStamp,
  signature, 
  message
) {
  try {
    yield put(fetchToken());
    const tokenResponse = yield call(
      RequestXhrUtil.request,
      `${SCRM_API}/login`,
      'post',
      UA({
        site_id: site_id,
        encrypted_type: 1,
        uim: uniqueIdMd5,
        suid: uid,
        timestamp: timeStamp,
        signature: signature
      })
    );
    yield put(receiveToken(
      tokenResponse,
      message
    ));
    const errorMessage = tokenResponse.errmsg;
    if (tokenResponse.errcode) {
      yield ToastUtil.showShort(errorMessage);
    }
  } catch (error) { 
    console.log(error, 'error')
    yield put(receiveToken(null, message));
    ToastUtil.showShort('网络发生错误，请重试');
  }
}

export function* watchRequestVerifySignature() {
  while (true) {
    const {
      signature,
      nonceStr,
      timeStamp,
      message
    } = yield take(types.REQUEST_VERIFY_SIGNATURE);
    yield fork(
      requestVerifySignature,
      signature,
      nonceStr,
      timeStamp,
      message
    );
  }
}

export function* watchRequestToken() {
  while (true) {
    const {
      site_id,
      uniqueIdMd5,
      uid,
      timeStamp,
      signature,
      message
    } = yield take(types.REQUEST_TOKEN);
    yield fork(
      requestToken,
      site_id,
      uniqueIdMd5,
      uid,
      timeStamp,
      signature,
      message
    );
  }
}
